<template>
  <div id="detail">
    <DetailNavBar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></DetailNavBar>
    <Scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
    >
      <DetailSwiper :topImages="topImages"></DetailSwiper>
      <DetailBaseInfo :goodsInfo="goodsInfo"></DetailBaseInfo>
      <DetailShopInfo :shop="shop"></DetailShopInfo>
      <DetailGoodsInfo :detailInfo="detailInfo"></DetailGoodsInfo>
      <DetailParamInfo ref="params" :paramInfo="paramInfo"></DetailParamInfo>
      <DetailCommentInfo
        ref="comment"
        :commentInfo="commentInfo"
      ></DetailCommentInfo>
      <GoodList ref="recommend" :goods="recommends"></GoodList>
    </Scroll>
    <DetailBottomBar @addToCart="addToCart"></DetailBottomBar>
    <BackTop @click.native="backClick" v-show="isShowBackTop"></BackTop>
    <!-- <Toast :message="message" :isShow="isShow"></Toast> -->
  </div>
</template>

<script>
import { itemListenerMixin, backTopMixin } from "common/mixin";

import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";

import GoodList from "components/content/goodList/GoodList";
import Scroll from "components/common/scroll/Scroll";
// import Toast from "components/common/toast/Toast";

import {
  getDetail,
  GoodsInfo,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    Scroll,
    GoodList,
    // Toast,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goodsInfo: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      // itemListenerLoad: null,
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      currentIndex: 0,
      message: "",
      isShow: false,
    };
  },
  mixins: [itemListenerMixin, backTopMixin],

  methods: {
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100);
    },
    contentScroll(position) {
      const positionY = -position.y;
      this.isShowBackTop = positionY > 1000;

      let length = this.themeTopYs.length;
      for (let i = 0; i < length; i++) {
        if (
          this.currentIndex !== i &&
          ((i < length - 1 &&
            positionY >= this.themeTopYs[i] &&
            positionY < this.themeTopYs[i + 1]) ||
            (i === length - 1 && positionY > this.themeTopYs[i]))
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
    },
    addToCart() {
      // 创建商品对象
      const product = {};

      // 给商品对象赋予应用值
      product.image = this.topImages[0];
      product.title = this.goodsInfo.title;
      product.desc = this.goodsInfo.desc;
      product.price = this.goodsInfo.realPrice;
      product.iid = this.iid;

      // 添加商品
      this.$store.dispatch("addCart", product).then((res) => {
        // console.log(res);
        this.$toast.show('加入购物车成功',1500)
      });
    },
  },

  activated() {
    this.$refs.scroll.refresh();
  },

  created() {
    // 保存传入的 iid
    this.iid = this.$route.params.id;

    // 根据 iid 请求详情数据
    getDetail(this.iid).then((res) => {
      // 获取数据
      const data = res.result;

      // 获取轮播图数据
      this.topImages = data.itemInfo.topImages;

      // 获取商品信息
      this.goodsInfo = new GoodsInfo(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 获取店铺信息
      this.shop = new Shop(data.shopInfo);

      // 保存商品详情数据
      this.detailInfo = data.detailInfo;

      // 获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      // 获取评论信息
      // console.log(data.rate.list[0])
      // console.log(DetailCommentInfo)
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }

      this.$nextTick(() => {
        this.themeTopYs = [];
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      });
    });

    getRecommend().then((res) => {
      this.recommends = res.data.list;
      // console.log(res)
    });
  },

  destroyed() {
    this.$bus.$off("itemImageLoad", this.detailImg);
  },
};
</script>

<style scoped>
.content {
  height: calc(100% - 44px);
  overflow: hidden;
}

#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.detail-nav {
  position: relative;
  background-color: #fff;
  z-index: 9;
}
</style>
